package com.small;

import java.util.HashMap;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.small.act.DiscountHomeActivity;
import com.small.act.FavDiscountActivity;
import com.small.act.map.NearDiscountActivity;
import com.small.interfaces.Task;
import com.small.interfaces.Util;

 
public class TabDiscountActivity extends TabActivity implements Task, OnClickListener {

	RadioButton mBut1, mBut2, mBut3;
	RadioGroup radioGroup;
	private TabHost mTabHost;
	private Intent tabIntent1, tabIntent2, tabIntent3;
	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;
	int mCurTabId = 0;
//	View View_White_Bg;

	boolean isOutOrInAnim = false;// 判断Tab页进入或退出动画
	boolean isNeedAnim = false;// 如果当前已经选中，再次点击是否需要动画

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_discount_view);
		prepareAnim();
		prepareIntent();
		initViews();
//		SysApplication.getInstance().addActivity(this);
	}

	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);
		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	private void prepareIntent() {
		tabIntent1 = new Intent(this, DiscountHomeActivity.class);
		tabIntent2 = new Intent(this, NearDiscountActivity.class);
		tabIntent3 = new Intent(this, FavDiscountActivity.class);  
	}

	/**
	 * 初始化数据
	 */ 
	private void initViews() {
		// 把这个Activity添加到集合里,从集合里面取,如果不这样添加回调回来的不是该Activity
		Util.removeActivity();
		Util.addActivity(this); 
		mTabHost = getTabHost();
		mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(tabIntent1)
				.setIndicator("tab1"));
		mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(tabIntent2)
				.setIndicator("tab2"));
		mTabHost.addTab(mTabHost.newTabSpec("tab3").setContent(tabIntent3)
				.setIndicator("tab3")); 
		radioGroup = (RadioGroup) this.findViewById(R.id.main_radio);
		mBut1 = (RadioButton) findViewById(R.id.radio_btn_home);
		mBut2 = (RadioButton) findViewById(R.id.radio_btn_gps);
		mBut3 = (RadioButton) findViewById(R.id.radio_btn_fav); 
		mBut1.setOnClickListener(this);
		mBut2.setOnClickListener(this);
		mBut3.setOnClickListener(this);  
		mTabHost.setCurrentTabByTag("tab1");
		mCurTabId = R.id.radio_btn_home;
		mBut1.setChecked(true);
		mBut1.setBackgroundResource(R.drawable.home_btn_bg_ao);
		Drawable drawableTop = getResources().getDrawable( R.drawable.btn_discount_press);
		mBut1.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null);

	}

	/**
	 * 下面按钮的默认未选中背景
	 */
	private void setDefaultImage() {

		final Drawable drawableTop1 = getResources().getDrawable(
				R.drawable.btn_discount);
		mBut1.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop1, null,
				null);

		final Drawable drawableTop2 = getResources().getDrawable(
				R.drawable.btn_gps);
		mBut2.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop2, null,
				null);

		final Drawable drawableTop3 = getResources().getDrawable(
				R.drawable.btn_fav);
		mBut3.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop3, null,
				null);

		 

		mBut1.setChecked(false);
		mBut2.setChecked(false);
		mBut3.setChecked(false);
		 
		mBut1.setBackgroundResource(R.drawable.home_btn_bg_tu);
		mBut2.setBackgroundResource(R.drawable.home_btn_bg_tu);
		mBut3.setBackgroundResource(R.drawable.home_btn_bg_tu);
		 
		mBut1.setTextColor(getResources().getColor(R.color.white));
		mBut2.setTextColor(getResources().getColor(R.color.white));
		mBut3.setTextColor(getResources().getColor(R.color.white));  
	}

	@Override
	// 接口回调
	public void onRefresh(int page, HashMap<String, Object> map) {

		switch (page) { 
 
		case 5:

			break;
		}
	} 
 
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.radio_btn_home: 
			preAnimation(v.getId());
			if (mCurTabId == v.getId()) {
				return;
			}
			mTabHost.setCurrentTabByTag("tab1");
			setButtonPressed(mBut1, R.drawable.btn_discount_press);
			afterAnimation(v.getId());
			break;
		case R.id.radio_btn_gps: 
			preAnimation(v.getId());
			if (mCurTabId == v.getId()) {
				return;
			}
			mTabHost.setCurrentTabByTag("tab2");
			setButtonPressed(mBut2, R.drawable.btn_gps_press);
			afterAnimation(v.getId());
			break;
		 
		case R.id.radio_btn_fav: 
			preAnimation(v.getId());
			if (mCurTabId == v.getId()) {
				return;
			}
			mTabHost.setCurrentTabByTag("tab3");
			setButtonPressed(mBut3, R.drawable.btn_fav_press);
			afterAnimation(v.getId());
			break; 
		} 
	} 

	// 按钮点击后状态修改
	private void setButtonPressed(RadioButton mBut, int resId) {
		mBut.setChecked(true);
		final Drawable drawableTop5Pressed = getResources().getDrawable(resId);
		mBut.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop5Pressed, null, null);
		mBut.setBackgroundResource(R.drawable.home_btn_bg_ao); 
	}

	// 判断是否需要加载动画，及动画的方向
	private void preAnimation(int Id) {
		setDefaultImage();

		if (mCurTabId == Id) {
			isNeedAnim = false;
		} else {
			isNeedAnim = true;
			if (mCurTabId < Id)
				isOutOrInAnim = true;
			else
				isOutOrInAnim = false;
			if (isOutOrInAnim)
				mTabHost.getCurrentView().startAnimation(left_out);
			else
				mTabHost.getCurrentView().startAnimation(right_out);
		} 
	}

	private void afterAnimation(int Id) {
		if (isNeedAnim) {
			if (isOutOrInAnim)
				mTabHost.getCurrentView().startAnimation(left_in);
			else
				mTabHost.getCurrentView().startAnimation(right_in);
			mCurTabId = Id;
		}
	} 
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
		return super.onKeyDown(keyCode, event);
	} 
}